<template>
  <a-modal
      v-model:visible="visible"
      :title="title"
      :mask-closable="false"
      :esc-to-close="false"
      :width="width >= 1800 ? 1800 : '100%'"
      draggable
      @before-ok="save"
      @close="reset"
  >
    <GiForm ref="formRef" v-model="form" :columns="columns"/>
  </a-modal>
</template>

<script setup lang="ts">
import {Message} from '@arco-design/web-vue'
import {useWindowSize} from '@vueuse/core'
import {getTodo, addTodo, updateTodo} from '@/apis/system/todo'
import {type ColumnItem, GiForm} from '@/components/GiForm'
import {useResetReactive} from '@/hooks'
import {useDict} from '@/hooks/app'

const emit = defineEmits<{
  (e: 'save-success'): void
}>()

const {width} = useWindowSize()

const dataId = ref('')
const visible = ref(false)
const isUpdate = computed(() => !!dataId.value)
const title = computed(() => (isUpdate.value ? '修改' : '新增'))
const containerRef = ref<HTMLElement | null>()
const formRef = ref<InstanceType<typeof GiForm>>()
const {
  priority_type,
  todo_type,
  todo_status_enum,
  todo_business_enum,
  todo_scope_enum
} = useDict('priority_type', 'todo_type', 'todo_status_enum', 'todo_business_enum', 'todo_scope_enum')

const [form, resetForm] = useResetReactive({
  // todo 待补充
  title: '',
  content: '',
  priority: '',
  type: '',
  businessType: '',
  isRemind: false,
  todoScope: [1],
  status: 1,
})

const columns: ColumnItem[] = reactive([
  {
    label: '标题',
    field: 'title',
    type: 'input',
    span: 24,
    props: {
      maxLength: 150,
      showWordLimit: true,
    },
    rules: [{required: true, message: '请输入标题'}],
  },
  {
    label: '内容',
    field: 'content',
    type: 'textarea',
    span: 24,
    props: {
      maxLength: 500,
      showWordLimit: true,
    },
    rules: [{required: true, message: '请输入内容'}],
  },
  {
    label: '优先级',
    field: 'priority',
    type: 'select',
    span: 24,
    props: {
      options: priority_type,
    },
    rules: [{required: true, message: '请选择优先级'}],
  },
  {
    label: '类型',
    field: 'type',
    type: 'select',
    disabled: () => {
      return form.status === 3
    },
    props: {
      options: todo_type,
    },
    rules: [{required: true, message: '请选择待办类型'}],
  },
  {
    label: '业务类型',
    field: 'businessType',
    type: 'select',
    span: 24,
    hide: () => {
      return form.type === 3
    },
    props: {
      options: todo_business_enum,
    },
  },
  {
    label: '开始时间',
    field: 'startTime',
    type: 'date-picker',
    props: {
      showTime: true,
    },
    span: 24,
  },
  {
    label: '截止时间',
    field: 'endTime',
    type: 'date-picker',
    props: {
      showTime: true,
    },
    span: 24,
  },
  {
    label: '待办通知范围（1：所有人；2：指定用户）',
    field: 'todoScope',
    type: 'select',
    span: 24,
    required: true,
    props: {
      options: todo_scope_enum,
    },
  },
  {
    label: '是否提醒',
    field: 'isRemind',
    type: 'select',
    span: 24,
    required: true,
    props: {
      type: 'round',
      checkedValue: true,
      uncheckedValue: false,
      checkedText: '是',
      uncheckedText: '否',
    },
  },
  {
    label: '提醒时间',
    field: 'remindTime',
    type: 'date-picker',
    props: {
      showTime: true,
    },
    span: 24,
  },
])

// 重置
// const reset = () => {
//   formRef.value?.formRef?.resetFields()
//   resetForm()
// }

// 保存
const save = async () => {
  const isInvalid = await formRef.value?.formRef?.validate()
  if (isInvalid) return false
    try {
      // 通知范围 所有人 去除指定用户
      form.todoUsers = form.todoScope === 1 ? null : form.todoUsers
      form.status = isDraft ? 1 : 3
      if (isUpdate.value) {
        await updateTodo(form, dataId.value)
        Message.success('修改成功')
      } else {
        await addTodo(form)
        Message.success('新增成功')
      }
      emit('save-success')
      return true
    } catch (error) {
      console.error(error)
      return false
    }
}

// 新增
const onAdd = async () => {
  resetForm()
  dataId.value = ''
  visible.value = true
}

// 修改
const onUpdate = async (id: string) => {
  resetForm()
  dataId.value = id
  const res = await getTodo(id)
  Object.assign(form, res.data)
}

defineExpose({onAdd, onUpdate})
</script>

<style scoped lang="scss"></style>
